extends Control

const UI_CARD_ITEM = preload("res://ui/ui_card/ui_card_item.tscn")
@onready var all_cards_grid_parent: GridContainer = $MainParent/Main/AllCards/ScrollContainer/GridContainer
@onready var equipped_cards_parent: VBoxContainer = $MainParent/Main/EquippedCards/VBoxContainer

@onready var description_label: Label = $MainParent/Main/Description

# select
var current_selecting_card: UICardItem = null


func open():
	init_all_cards()
	init_equipped_cards()
	

func init_all_cards():
	var all_cards_data = GlobalData.get_items_by_type([ItemBaseConfig.ItemType.Card])
	
	for card_data in all_cards_data:
		var inst = UI_CARD_ITEM.instantiate()
		all_cards_grid_parent.add_child(inst) 
		inst.init_by_data(card_data)
		
		inst.signal_mouse_enter.connect(handler_card_mouse_enter)
		inst.signal_mouse_exit.connect(handler_card_mouse_exit)
		
		if card_data in GlobalData.player_data.equip_cards:
			inst.set_selected()
		else:
			inst.set_unselected()
	
	
func init_equipped_cards():
	var equipped_cards_data = GlobalData.player_data.equip_cards
	
	for card_data in equipped_cards_data:
		var inst = UI_CARD_ITEM.instantiate()
		equipped_cards_parent.add_child(inst) 
		inst.init_by_data(card_data)
		
		inst.signal_mouse_enter.connect(handler_card_mouse_enter)
		inst.signal_mouse_exit.connect(handler_card_mouse_exit)
		
		inst.set_unselected()
		

func handler_card_mouse_enter(card: UICardItem):
	card.set_selecting()
	description_label.text = card.item_data.item_config.name + "\n" + card.item_data.item_config.item_desc
	current_selecting_card = card
	
	
func handler_card_mouse_exit(card: UICardItem):
	card.set_unselecting()
	description_label.text = ""
	current_selecting_card = null


func _process(delta: float) -> void:
	process_equip_card()
	

func process_equip_card():
	if not Input.is_action_just_pressed("use") or not current_selecting_card: return
	
	if current_selecting_card.item_data in GlobalData.player_data.equip_cards:
		GlobalData.player_data.equip_cards.erase(current_selecting_card.item_data)
		
		var equipped_card = find_card_in_equipcards(current_selecting_card.item_data)
		if equipped_card:
			equipped_card.queue_free()
		
		var card_in_allcards = find_card_in_allcards(current_selecting_card.item_data)
		card_in_allcards.set_unselected()
	else:
		if GlobalData.player_data.equip_cards.size() >= GlobalData.player_data.config.max_equip_card_count: return
		
		GlobalData.player_data.equip_cards.append(current_selecting_card.item_data)
		
		var card_in_allcards = find_card_in_allcards(current_selecting_card.item_data)
		card_in_allcards.set_selected()
		
		var inst = UI_CARD_ITEM.instantiate()
		equipped_cards_parent.add_child(inst) 
		inst.init_by_data(current_selecting_card.item_data)
		inst.signal_mouse_enter.connect(handler_card_mouse_enter)
		inst.signal_mouse_exit.connect(handler_card_mouse_exit)
		

func find_card_in_allcards(item_data: ItemData) -> Control:
	for index in range(all_cards_grid_parent.get_child_count()):
		var child = all_cards_grid_parent.get_child(index)
		if child.item_data == item_data:
			return child
	
	return null
	
	
func find_card_in_equipcards(item_data: ItemData) -> Control:
	for index in range(equipped_cards_parent.get_child_count()):
		var child = equipped_cards_parent.get_child(index)
		if child.item_data == item_data:
			return child
	
	return null
	
	
	
	
	
